package com.fish.admin.util;

import cn.hutool.core.collection.CollUtil;
import com.fish.admin.module.system.domain.bo.SysMenuBO;
import com.fish.common.core.base.Meta;
import com.fish.common.core.base.Router;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lindy */

public class ConvertUtils {

	private static final Integer YES = 1;

	private static final Integer NO = 0;


	public static void setChild(SysMenuBO parent, List<SysMenuBO> menus) {
		List<SysMenuBO> child = menus.stream()
				.filter(m -> parent.getId().equals(m.getParentId()))
				.sorted(Comparator.comparing(SysMenuBO::getSort))
				.collect(Collectors.toList());
		if (!CollUtil.isEmpty(child)) {
			parent.setChildren(child);
			child.parallelStream().forEach(c -> {
				setChild(c, menus);
			});
		}
	}


	public static List<Router> router(List<SysMenuBO> resource) {
		List<Router> routers = resource.stream().map(m -> {
			Router r = new Router();
			r.setId(m.getId());
			r.setPermission(m.getPermission());
			r.setName(m.getName());
			r.setAppId(m.getAppId());
			r.setRedirect(m.getRedirect());
			r.setPath(m.getPath());
			r.setComponent(m.getComponent());

			Meta meta = new Meta();
			meta.setTitle(m.getTitle());
			meta.setIcon(m.getIcon());
			meta.setType(m.getType());
			meta.setAuthorityTyp(m.getAuthorityType());

			meta.setAffix(YES.equals(m.getAffix()));
			meta.setFullPage(YES.equals(m.getFullPage()));
			meta.setHidden(YES.equals(m.getHidden()));
			meta.setHiddenBreadcrumb(YES.equals(m.getHiddenBreadcrumb()));
			meta.setTag(m.getTag());
			meta.setActive(m.getActive());
			meta.setColor(m.getColor());
			meta.setSort(m.getSort());
			r.setMeta(meta);

			List<SysMenuBO> menuChildren = m.getChildren();
			if (CollUtil.isNotEmpty(menuChildren)) {
				List<Router> children = router(menuChildren);
				r.setChildren(children);
			}
			return r;
		}).collect(Collectors.toList());
		return routers;
	}

}
